package com.supreme.commonmybatis.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.supreme.commonmybatis.interceptor.RangeQueryInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:mybatis-plus.properties")
@MapperScan(value = {"com.supreme.commonmybatis.mapper"})
public class MybatisPlusConfig {

    @Value("${spring.profiles.active}")
    private String springProfilesActive;

    @Bean
    public RangeQueryInterceptor rangerQueryInterceptor() {
        return new RangeQueryInterceptor();
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        System.out.println("========== MybatisPlusInterceptor ==========: " + springProfilesActive);
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //分页拦截器
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        //乐观锁拦截器
        OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);

        //自定义拦截器
//        interceptor.addInnerInterceptor(rangerQueryInterceptor());

        return interceptor;
    }
}
